package com.xunshibao.action;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunshibao.entity.info.TutorInfo;

@Controller
@RequestMapping(value = "/xxxxx/*")
public class SampleController {
    @RequestMapping("date/{value}")
    public String save(TutorInfo info, String ipaddress) {
        return null;
    }

    @RequestMapping("date/{id}")
    public String view(@PathVariable @DateTimeFormat(iso=ISO.DATE) Long id) {
        return null;
    }

    @RequestMapping(value="entity", method=RequestMethod.POST)
    public @ResponseBody String withEntity(HttpEntity<String> entity) {
        return "Posted request body '" + entity.getBody() + "'; headers = " + entity.getHeaders();
    }
    
    @RequestMapping(value="body", method=RequestMethod.POST)
    public @ResponseBody String withBody(@RequestBody String body) {
        return "Posted request body '" + body + "'";
    }
    
    @RequestMapping(value="path/{var}")
    public @ResponseBody String withPathVariable(@PathVariable String var) {
        return "Obtained 'var' path variable value '" + var + "'";
    }

    @RequestMapping(value="header")
    public @ResponseBody String withHeader(@RequestHeader String Accept) {
        return "Obtained 'Accept' header '" + Accept + "'";
    }

    @RequestMapping(value="cookie")
    public @ResponseBody String withCookie(@CookieValue String openid_provider) {
        return "Obtained 'openid_provider' cookie '" + openid_provider + "'";
    }
    
    @RequestMapping(value="param")
    public @ResponseBody String withParam(@RequestParam("foo") String foo) {
        return "Obtained 'foo' query parameter value '" + foo + "'";
    }

    @RequestMapping(value="group")
    public @ResponseBody String withParamGroup(TutorInfo bean) {
        return "Obtained parameter group " + bean;
    }
    @RequestMapping(method=RequestMethod.POST)
    public String create(TutorInfo account, BindingResult result) {
        if (result.hasErrors()) {
            return "account/createForm";
        }
        return "redirect:/account/" + account.getId();
    }
}
